package ext.tianma.project;

import java.util.Enumeration;

import wt.fc.Persistable;
import wt.lifecycle.State;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.project.Role;
import wt.team.Team;
import wt.team.TeamException;
import wt.team.TeamHelper;
import wt.team.TeamManaged;
import wt.util.WTException;

import com.ptc.core.ui.validation.DefaultSimpleValidationFilter;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationStatus;

public class UpdateProjectActionFilter extends DefaultSimpleValidationFilter {
	public UIValidationStatus preValidateAction(UIValidationKey uivalidationkey, UIValidationCriteria uivalidationcriteria) {
		Object obj = uivalidationcriteria.getContextObject().getObject();
		UIValidationStatus uiv = UIValidationStatus.HIDDEN;
		State state = null;
		Persistable pbo = (Persistable) obj;
		Team team;
		try {
			wt.org.WTPrincipal wtp = wt.session.SessionHelper.manager.getPrincipal();
			wt.projmgmt.admin.Project2 proj = (wt.projmgmt.admin.Project2)pbo;
			if(proj.getOwner().equals(wtp)){
				return UIValidationStatus.ENABLED; 
			}
			/*
			team = TeamHelper.service.getTeam((TeamManaged) pbo);
			Role role = Role.toRole("PM");
			
			Enumeration<WTPrincipalReference> members = team.getPrincipalTarget(role);
			while (members.hasMoreElements()) {
				WTPrincipalReference pr = members.nextElement();
				WTPrincipal principal = pr.getPrincipal();
				if (wtp.equals(principal) || wtp == principal) {
					return UIValidationStatus.ENABLED;
				}
			}
			*/
		} catch (WTException e) {
			e.printStackTrace();
		}
		return uiv;
	}
}
